package com.eshop.eshop2021_v1.common;

import com.eshop.eshop2021_v1.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author: 若若叶
 * @date: 2021/10/11 9:03 FileName: LoginInterceptor Description:
 */
public class LoginInterceptor implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    HttpSession session = request.getSession();
    // 判断seesion中有没有登录用户的信息
    User loginUser = (User) session.getAttribute("loginUser");
    if (null == loginUser) {
      response.sendRedirect("/user/toLogin");
      return false; // 不允许请求继续执行controller代码
    }
    return true;
  }
}
